<?php

class BlogController extends Base_Controller
{

   public function init()
    {
    	parent::init();
    }

	public function indexAction()
    {
        // action body
        $this->logger->debug("Entering  ".__METHOD__);
        $blogService = Application_Model_BlogService::getInstance();        
        $uid=null;
    	if(isset($_SESSION['userinfo'])) {
    		$uid = $_SESSION['userinfo']['uid'];
    	}
    	
    	$tagId = isset($_GET['tagId'])?$_GET['tagId']:null;
    	$catId = isset($_GET['catId'])?$_GET['catId']:null;
		$friendId = isset($_GET['mId'])?$_GET['mId']:null;
    	
		
		$blogcount = $blogService->getBlogCount($uid, $tagId, $catId, $friendId);  
		 
		$page  = new Application_Model_PagingUtil($this->_request,$blogcount);  
		
		$pages = $page->calculatePage();  
				
		
		$blogs = $blogService->fetchBlogs($uid, $tagId, $catId, $friendId, $pages['pagestart'] - 1, $pages['perpage']);
        
    	$tagService = Application_Model_TagService::getInstance();
    	$selectedTag=$tagService->getTag($tagId);
	    //$this->view->selectedTag=$selectedTag;
	    
	    $catService = Application_Model_CategoryService::getInstance();	        
	    $categories=$catService->getAllCategories($uid);
	    $selectedCategory=$catService->getCategory($catId);
		
		$this->prepareTags($uid);
		$this->view->assign(array('pages'=>$pages
		,'allBlogs'=>$blogs
		,'selectedTag'=>$selectedTag
		,'categories'=>$categories
		,'selectedCategory'=> $selectedCategory)); 
     	
    }
    
    /**
     * 
     * blog 主页的所有tag信息
     * @param unknown_type $uid
     */
    private function prepareTags($uid){
    	$tagService = Application_Model_TagService::getInstance();
    	$tags = $tagService->getAllTags($uid);
    	$this->view->assign(array('tags'=>$tags)); 
    }
   
    /**
     * 
     * 保存博客，新建或者更新
     */
    public function saveAction()
    {
    	if(isset($_SESSION['userinfo'])) {
    		$uid = $_SESSION['userinfo']['uid'];
        	
	    	$request = $this->getRequest();
	        if($this->getRequest()->isPost())
	        {	        	
	        	$blog = new Application_Model_BusiInfo_Blog();
				$blog->setBid($_POST['bid']);
	        	$blog->setTitle($_POST['title']);
	        	$blog->setContent($_POST['content']);
				$this->logger->debug("save blog: ".$_POST['content']);
	        	$blog->setTags($_POST['tag']);
	        	$blog->setCategory($_POST['category']);
				if (Application_Model_Constants::PRIVILEGE_TYPE_PRIVATE_STR == $_POST['privilege']) {
					$blog->setPrivilegeType(Application_Model_Constants::PRIVILEGE_TYPE_PRIVATE);
				} else {
					$blog->setPrivilegeType(Application_Model_Constants::PRIVILEGE_TYPE_PUBLIC);
				}
	        	$blogService = Application_Model_BlogService::getInstance();
				if ($_POST['bid'] == "") {
					$blogService->create($uid, $blog);
				} else {
					$blogService->save($uid, $blog);
				}
	        }
    	}
 
        	//return $this->_helper->redirector('index');
        $this->_redirect("/blog");
    }

	/**
	 * 
	 * 删除博客
	 */
    public function deleteAction()
    {
    	$this->logger->info(__METHOD__." Entering");
    	if(isset($_SESSION['userinfo'])) {
    		$uid = $_SESSION['userinfo']['uid'];
 			$blogId = $_GET['blogId'];
			$blogService = Application_Model_BlogService::getInstance();
			$blogService->delete($uid, $blogId);
    	}
 
        $this->logger->info(__METHOD__." Existing");
        $this->_redirect("/blog");
    }
    
    /**
     * 
     * 显示一边博客
     */
    public function viewAction()
    {
    	$this->logger->info(__METHOD__." Entering");
    	if(isset($_SESSION['userinfo'])) {
    		$uid = $_SESSION['userinfo']['uid']; 
    	}
    	$blogId = $_GET['blogId'];
    	$this->logger->debug("blog id = ".$blogId);
 		$blogService = Application_Model_BlogService::getInstance();
		$blog = $blogService->getBlog($blogId);
		
		$commentService = Application_Model_CommentService::getInstance();
		$comments = $commentService->getComments(Application_Model_Constants::CONTENT_TYPE_BLOG, $blogId);
        
        $this->view->assign(array('blog'=>$blog, 'comments'=>$comments)); 
		
        $this->logger->info(__METHOD__." Existing");        
    }
    
    /**
     * 
     * Edit the blog. Called when click edit in the blog view page
     */
    public function editAction() {
    	
    }
   
}

